//
//  NewBaseSingleAlertView.m
//  Yanwei
//
//  Created by Believer Just on 2018/12/5.
//  Copyright © 2018年 DCloud. All rights reserved.
//

#import "NewBaseSingleAlertView.h"
#import "UIView+Util.h"

@interface NewBaseSingleAlertView ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *hLineView;
@property (nonatomic, strong) UIView *vLineView;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *sureButton;
@property (nonatomic, assign) CGFloat heightPagn;

@end

@implementation NewBaseSingleAlertView

- (instancetype)initWithFrame:(CGRect)frame type:(NewBaseSingleAVType)type titleString:(NSString *)titleString {
    self = [super initWithFrame:frame];
    if (self) {
        [self initView:type titleString:titleString];
    }
    return self;
}

- (void)initView:(NewBaseSingleAVType)type titleString:(NSString *)titleString {
    if (ScreenWidth < 375) {
        self.heightPagn = WIDTH;
    } else {
        self.heightPagn = 1;
    }
    
    switch (type) {
        case NewBaseSingleAVTypeBC:
        case NewBaseSingleAVTypePersonHome: {
            self.titleLabel.text = titleString;
            self.sureButton.hidden = NO;
            break;
        }
        default:
            break;
    }
}


#pragma mark - progress
- (void)progressSingleAlertViewButton:(UIButton *)sender {
    switch (sender.tag) {
        case 0: { // 取消
            [self hideView];
            if (self.newBaseSingleAlertViewBlock) {
                self.newBaseSingleAlertViewBlock(NO);
            }
            break;
        }
        case 1: { // 确认
            if (self.newBaseSingleAlertViewBlock) {
                self.newBaseSingleAlertViewBlock(YES);
            }
            break;
        }
        default:
            break;
    }
}


#pragma mark - 懒加载
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.cornerRadius = 5;
        [self addSubview:_bgView];
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.centerX.equalTo(self);
            make.width.mas_equalTo(282 *self.heightPagn);
            make.height.mas_equalTo(130 *self.heightPagn);
        }];
    }
    return _bgView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        [_titleLabel setFont:APPFONT(14)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = Color34;
        _titleLabel.numberOfLines = 2;
        [self.bgView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgView).with.offset(10 *self.heightPagn);
            make.right.equalTo(self.bgView).with.offset(- 10 *self.heightPagn);
            make.top.equalTo(self.bgView).with.offset(22 *self.heightPagn);
            make.bottom.equalTo(self.hLineView.mas_top).with.offset(- 10 *self.heightPagn);
        }];
    }
    return _titleLabel;
}
- (UIView *)vLineView {
    if (!_vLineView) {
        _vLineView = [UIView new];
        _vLineView.backgroundColor = Color217;
        [self.bgView addSubview:_vLineView];
        [_vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self.bgView);
            make.height.mas_equalTo(0.5);
            make.bottom.equalTo(self.cancelButton.mas_top);
        }];
    }
    return _vLineView;
}
- (UIView *)hLineView {
    if (!_hLineView) {
        _hLineView = [UIView new];
        _hLineView.backgroundColor = Color217;
        [self.bgView addSubview:_hLineView];
        [_hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.bgView);
            make.width.mas_equalTo(0.5);
            make.bottom.equalTo(self.bgView);
            make.top.equalTo(self.vLineView.mas_bottom);
        }];
    }
    return _hLineView;
}
- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:Color80 forState:UIControlStateNormal];
        [_cancelButton.titleLabel setFont:APPFONT(14)];
        _cancelButton.tag = 0;
        [_cancelButton addTarget:self action:@selector(progressSingleAlertViewButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:_cancelButton];
        [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.bottom.equalTo(self.bgView);
            make.height.mas_equalTo(48 *self.heightPagn);
            make.width.equalTo(self.bgView).multipliedBy(0.5);
        }];
    }
    return _cancelButton;
}
- (UIButton *)sureButton {
    if (!_sureButton) {
        _sureButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [_sureButton setTitleColor:Color175030 forState:UIControlStateNormal];
        [_sureButton.titleLabel setFont:APPFONT(14)];
        _sureButton.tag = 1;
        [_sureButton addTarget:self action:@selector(progressSingleAlertViewButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:_sureButton];
        [_sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.and.bottom.equalTo(self.bgView);
            make.height.mas_equalTo(48 *self.heightPagn);
            make.width.equalTo(self.bgView).multipliedBy(0.5);
        }];
    }
    return _sureButton;
}

@end
